<?php

if ($_SESSION['USER_SID']) {
	redirect("index.php");
}	

require_once ("../include/recaptchalib.php");

$mailhide_pubkey = '6LfbCecSAAAAALJKmMW5YvzCQ3P5K7wfKaWg_RyP';
$mailhide_privkey = '6LfbCecSAAAAAG7F7p0Eb60-fldAXeza1ufCEaGs';

# the response from reCAPTCHA
$resp = null;
# the error code from reCAPTCHA, if any
$error = null;


	if (!isset($_POST['is_submitted'])) {
    	$_POST['is_submitted'] = '';
	}
	if (!isset($_POST['email'])) {
		$_POST['email'] = '';
	}
	if (!isset($_POST['password'])) {
		$_POST['password'] = '';
	}
	if (!isset($error_message)) {
		$error_message = '';
	}
	

	
	if ($_POST['is_submitted']) {
		
		$error_message = '';
		if ($_POST["recaptcha_response_field"]) {
        $resp = recaptcha_check_answer ($mailhide_privkey,
                                        $_SERVER["REMOTE_ADDR"],
                                        $_POST["recaptcha_challenge_field"],
                                        $_POST["recaptcha_response_field"]);

        if (!$resp->is_valid) {
        
                $error = $resp->error;
				$error_message .= __ERM_NO_VALID_CAPTCHA;
        }
	}else{
		$error_message .= __ERM_NO_CAPTCHA;
	}
 
		
		
		if (!$_POST['email']) $error_message .= __ERM_NO_EMAIL;
		
		
		
		else $_POST['email'] = escape($_POST['email']);
		
		if (checkEmail($_POST['email'])== 0) $error_message .= __ERM_EMAIL_NO_VALID;
		
		$sql = "SELECT
					*
				   FROM
					 users
				   WHERE
					 email = \"".escape($_POST['email'])."\"
				";
		$result = query($sql);
		
		if ($row = mysql_fetch_object($result)) $error_message = __ERM_EMAIL_BISI;
				
		
		if (!$_POST['password']) $error_message .= __ERM_NO_PASS;
		else $_POST['password'] = escape($_POST['password']);
		
		$pwd = $_POST['password'];

		if( strlen($pwd) > 20 ) {
			$error_message .= __ERM_PASS_LONG;
		}
		
		if( strlen($pwd) < 6 ) {
			$error_message .= __ERM_PASS_SHORT;
		}
		
		if( !preg_match("#[0-9]+#", $pwd) ) {
			$error_message .= __ERM_PASS_NO_NUMBER;
		}
		
		
		if( !preg_match("#[a-z]+#", $pwd) ) {
			$error_message .= __ERM_PASS_NO_LETTER; 
		
		}
		
		
		if( !preg_match("#[A-Z]+#", $pwd) ) {
			$error_message .= __ERM_PASS_NO_CAPS; 
		
		}
		
		if (!$_POST['repassword']) $error_message .= __ERM_PASS_COMFIRM;
		else $_POST['repassword'] = escape($_POST['repassword']);
		
		if ($_POST['password'] != $_POST['repassword']) $error_message .= __ERM_2_PASS;
		
		if (!$_POST['phone']) $error_message .= __ERM_NO_PHONE;
		
		if (!$_POST['iagree']) $error_message .= __ERM_IAGREE;
		
		
		#OK
		if (!$error_message) {

				
				$sql = "INSERT INTO users
			(email,h_password,full_name,accsess,phone,entity,lang)
					    VALUES
		    ('".escape($_POST['email'])."','".md5(escape($_POST['password']))."','".escape($_POST['fullname'])."','1' ,'".escape($_POST['phone'])."','".escape($_POST['entity'])."','".$_SESSION["lang"]."')
					   
					";
				query($sql);
				
				
				$uid = mysql_insert_id();
				$_SESSION['isLoggedIn'] = true;
				$_SESSION['USER_SID'] = $uid;
				$_SESSION['USERNAME'] = $_POST['fullname'];
				$_SESSION['ACCESS_SID'] = 1;
				
				  
				  mail( ADMINISTRATOR_EMAIL,
				   __REGISTER_ADMIN_SUBJECT,
					sprintf(__REGISTER_ADMIN_MESSAGE, $_SESSION['USERNAME']),
						HTML_HEADERS."From: ".DOMAIN_NAME." <".NO_REPLY_EMAIL.">");
				  mail( $_POST['email'],
				  __REGISTER_EMAIL_SUBJECT,
					sprintf(__REGISTER_EMAIL_MESSAGE, $_SESSION['USERNAME']),
					HTML_HEADERS."From: ".DOMAIN_NAME." <".NO_REPLY_EMAIL.">");
				
				
				# За проверка на валидност на сесията
				$salt = substr(md5(date("F")), 8);
				$_SESSION['LOGGEDIN'] = md5($uid.$salt);
			
				if ($_SESSION['ACCESS_SID'] == 1) {
					
					redirect(HTML_BASE.$_SESSION["lang"]."/dashboard/");
					
				} elseif ($_SESSION['ACCESS_SID'] == 2) {
					
					redirect(HTML_BASE.$_SESSION["lang"]."/law/");
					
				} elseif ($_SESSION['ACCESS_SID'] > 2) {
					
					redirect(HTML_BASE_ADMIN);
					
				}
				
			
			
		}
	}
	
	head(__register);

?>
<script type="text/javascript">
 var RecaptchaOptions = {
    theme : 'white'
 };
</script>
 	
	
 
	<div id="loginform">
			
			<h3><?=__register?></h3><br><br>
            
            
            <?php if ($error_message) { ?>
            <div class="message red">
                <?=$error_message?>
            </div>
            <?php } ?>
            
            <form action="<?=HTML_BASE.$_SESSION["lang"].'/register/'?>" method="post" enctype="multipart/form-data">
            
            
            
            
            <input type="hidden" name="is_submitted" value="1" />
			
            <table style="margin:auto;" width="70%" border="0" cellspacing="0" cellpadding="0">
              <tr>
                <td>
                <label for="fullname" class="required"><?=__FULL_NAME?></label><br>
            <input class="inputbox" name="fullname" type="text" value="<?=$_POST['fullname']?>" >
            </td>
               <td>
              		<label for="email" class="required">E-mail</label><br>
            <input class="inputbox" name="email" type="text" value="<?=$_POST['email']?>" ><br>
            <span class="tip"><?=__EMAIL_TIP?></span>  
              </td>
              </tr>
              <tr>
                <td><label for="entity" class="required"><?=__entity_NAME?></label><br>
            <input class="inputbox" name="entity" type="text" value="<?=$_POST['entity']?>" ></td>
                <td>
                	<label for="password" class="required"><?=__PASS?></label><br>
            <input class="inputbox" name="password" type="password" value="<?=$_POST['password']?>" ><br>
            <span class="tip"><?=__PASS_TIP?></span> 
                </td>
              </tr>
              <tr>
                <td><label for="phone" class="required"><?=__PHONE?></label><br>
            <input class="inputbox" name="phone" type="text" value="<?=$_POST['phone']?>" ><br>
            <span class="tip"><?=__PHONE_TIP?></span></td>
                <td>
               <label for="password" class="required"><?=__PASS_COMFIRM?></label><br>
            <input class="inputbox" name="repassword" type="password" value="<?=$_POST['repassword']?>" >
                
                </td>
              </tr>
              
              
               <tr>
                <td>
                <?php
		 		echo recaptcha_get_html($mailhide_pubkey, $error);
		   		?>
                </td>
                <td>
               <label>
                <input type="checkbox" value="1" name="iagree" >
                <?=__I_AGREE_WITH?> <a target="_blank" href="<?=HTML_BASE.$_SESSION["lang"].'/terms/'?>"><?=__TERMS?></a>
               </label>
            
			<br><br><br>
			<input  type="submit" name="submit" id="submit" class="button" value="<?=__register?>">
                
                </td>
              </tr>
              
            </table>
			
			</form>
            
	</div>
    

<?php
foot();
?>